package com.computergames.server.service.getCurrentPlayerStrategy;

import com.computergames.server.server.session.RoomSessionFactory;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class Play_GetChessCurrentPlayer implements Play_GetCurrentPlayerStrategy {
    private final Map<String, String> NextPlayer =new ConcurrentHashMap<>();

    @Override
    public boolean getCurrentPlayer(String roomName, String from) {
        Set<String> members = RoomSessionFactory.getRoomSession()
                .getMembers(roomName);
        if (members.size() == 2) {
            if (NextPlayer.get(roomName) == null) {
                NextPlayer.put(roomName, from);
                return true;
            } else {
                if (NextPlayer.get(roomName).equals(from)) {
                    return false;
                } else {
                    NextPlayer.put(roomName, from);
                    return true;
                }
            }
        }else{
            return false;
        }
    }
}
